// pages/myactivity/releaseactivity.js
var citylist = require('../../utils/citylist.js').citylist
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.imgurl,
    updateList: {
      idcard: { // 上传的图片
        showVideo: false,
        showImg: false,
      }
    },
    citylist: citylist,
    services: [{
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }, {
      text: '去火星散步',
      selected: 0,
    }],
    org: ['组织1', '组织1', '组织1', '组织1', '组织1'],
    orgtype: ['活动1', '活动1', '活动1', '活动1','活动1'],
    orgpartnertype:[{text:'所有人',typeset:2},{text:'组织认证成员',typeset:1}],
    acinfo:{
      selectedService:[],
      remark:''
    },
    // 选择器隐藏
    showPro:0, 
    showPro: 0,
    showCity: 0,
    showOrg: 0,
    showOrgtype: 0,
    showOrgPartnerType: 0,
    //
  },
  // 上传
  update: function (e) {
    var that = this
    var o = e.currentTarget.dataset;
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      compressed: 'true',
      maxDuration: 10,
      success: function (res) {
        console.log(res)
        var imgurl = res.tempFilePaths[0]
        that.upload(o,imgurl)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 上传
  upload: function (o, imgurl){
    var that = this
    var updateList = that.data.updateList
    wx.uploadFile({
      url:app.globalData.url+ 'Organize/upload',
      filePath: imgurl,
      name: 'image',
      success:function(res){
        if(!res.data.code){
          var data = JSON.parse(res.data)
          if (data.code == 1) {
            updateList[o.uptype].showImg = true
            updateList[o.uptype].imgUrl = data.path
            that.setData({
              updateList: updateList
            })
          } else {

          }
        }
       
      
      }
    })
  },
   // 获取活动名称
  getName: function (e) {
    var acinfo = this.data.acinfo
    acinfo.name = e.detail.value
    this.setData({
      acinfo: acinfo
    })
  },
  // 选择活动特色
  selectService: function (e) {
    var o = e.currentTarget.dataset
    var acinfo = this.data.acinfo
    var services = this.data.services
    services[o.index].selected = !services[o.index].selected
    this.setData({
      services: services
    })
  },
  // 获取活动经费
  getMoney: function (e) {
    var acinfo = this.data.acinfo
    acinfo.money = e.detail.value
    this.setData({
      acinfo: acinfo
    })
  },
  // 获取邮箱
  getEmail: function (e) {
    var acinfo = this.data.acinfo
    acinfo.email = e.detail.value
    this.setData({
      acinfo: acinfo
    })
  },
 
  // 获取地址
  getAddr: function (e) {
    var acinfo = this.data.acinfo
    acinfo.address = e.detail.value
    this.setData({
      acinfo: acinfo
    })
  },
  // 获取活动说明
  getRemark: function (e) {
    var acinfo = this.data.acinfo
    acinfo.remark = e.detail.value
    this.setData({
      acinfo: acinfo
    })
  },
 
  // 提示弹窗
  showmodal: function (es) {
    wx.showModal({
      title: '提示',
      content: es,
      showCancel: false,
      confirmColor: '#ff8e03'
    })
  },
  //显示省
  showPro: function () {
    this.setData({
      showPro: !this.data.showPro,

    })
  },
  // 选择省
  selectPro:function(e){
    var o = e.currentTarget.dataset
    var citylist=this.data.citylist
    this.setData({
      proIndex:o.index,
      selectedPro: citylist[o.index],
      selectedCity:null
    })
  },
  //显示市
  showCity: function () {
    var selectedPro = this.data.selectedPro
    if (selectedPro) {
      this.setData({
        showCity: !this.data.showCity,

      })
    } else {
      this.showmodal("您还没有选择省")
    }
  },
  // 选择市
  selectCity:function(e){
    var o = e.currentTarget.dataset
    var selectedPro = this.data.selectedPro
    this.setData({
      cityIndex: o.index,
      selectedCity: selectedPro.children[o.index],
    })
  },
  //显示活动组织方
  showOrg: function () {
    this.setData({
      showOrg: !this.data.showOrg,

    })
  },
  // 选择活动组织方
  selectOrg:function(e){
    var o = e.currentTarget.dataset
    var org = this.data.org
    this.setData({
      orgIndex: o.index,
      selectedOrg: org[o.index],
    })
  },
  //显示活动类型
  showOrgtype: function () {
    this.setData({
      showOrgtype: !this.data.showOrgtype,

    })
  },
  // 选择活动类型
  selectOrgType:function(e){
    var o = e.currentTarget.dataset
    var orgtype = this.data.orgtype
    this.setData({
      orgtypeIndex: o.index,
      selectedOrgtype: orgtype[o.index].name,
    })
  },
  //显示可报名人员
  showOrgPartnerType: function () {
    this.setData({
      showOrgPartnerType: !this.data.showOrgPartnerType,

    })
  },
  // 选择可报名人员
  selectOrgPartnerType: function (e) {
    var o = e.currentTarget.dataset
    var orgpartnertype = this.data.orgpartnertype
    this.setData({
      opIndex: o.index,
      selectedOrgPartnerType: orgpartnertype[o.index],
    })
  },
  // 关闭选项
  close:function(){
    this.setData({
      showPro:0,
      showCity:0,
      showOrg:0,      
      showOrgtype:0,
      showOrgPartnerType:0
    })
  },
  summit: function () {
    var that = this
    var acinfo = this.data.acinfo
    var services = this.data.services
    var updateList = this.data.updateList
    acinfo.selectedService = []
    for (var i = 0; i < services.length; i++) {
      if (services[i].selected == 1) {
        acinfo.selectedService.push(services[i].name)
      }
    }

    console.log(acinfo)
    if (!updateList.idcard.imgUrl || updateList.idcard.imgUrl == '') {
      this.showmodal('请上传封面图片')
    } else 
    if (!updateList.idcard.imgUrl || updateList.idcard.imgUrl == '') {
      this.showmodal('请上传封面图片')
    } else if (!acinfo.name || acinfo.name == '') {
      this.showmodal('活动名称不能为空')
    } else if (!acinfo.selectedService || acinfo.selectedService.length == 0) {
      this.showmodal('请选择活动特色')
    } else if (!acinfo.money || acinfo.money == '') {
      this.showmodal('活动经费不能为空')
    } else if (!acinfo.remark || acinfo.remark == '') {
      this.showmodal('详细说明不能为空')
    } else {

      wx.request({
        url: app.globalData.url + 'Index/addhuodong',
        data: {
          wxopenid: wx.getStorageSync('openId'),
          hd_starttime: "1521505980",
          organize_id: that.data.selectedOrg.id,  // 组织id
          hd_name: acinfo.name, // 活动名称
          hd_money: acinfo.money, // 活动经费
          hd_province: that.data.selectedPro.name,  // 省
          hd_city: that.data.selectedCity.name, // 市
          hd_type: that.data.selectedOrgtype, // 活动类型
          hd_tags: acinfo.selectedService.join(','),  // 活动特色
          hd_county: null,
          hd_address: acinfo.address, // 活动地址
          hd_phone: null,
          hd_pic: updateList.idcard.imgUrl, // 活动封面
          hd_content: acinfo.remark,  // 详细说明
          hd_set: that.data.selectedOrgPartnerType.typeset, // 1，表示组织成员， 2,表示 任何人
          hd_endtime: "1522110780",  
          sort_title: that.data.selectedOrg.sort_title  // 组织名称
        },
        // method: 'POST',
        success: function (res) { 
          if (res.data.msg == '发布成功'){
            wx.showToast({
              title: res.data.msg,
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function(res) {
                wx.navigateBack({
                  delta: 1,
                })
              },
            })
          }
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: app.globalData.url+ '',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(citylist)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    // 活动特色
    wx.request({  
      url: app.globalData.url + 'Index/hdtags',
      success:res=>{
        var services = res.data.hd_tags
        for(var i = 0; i < services.length; i++){
          services[i].selected = 0
        }

        console.log(services)
        that.setData({
          services: services
        })
      }
    })
    // 活动类型
    wx.request({
      url: app.globalData.url+'Index/hdtype',
      success: res => {
        console.log(res)
        var orgtype = res.data.hd_type
        that.setData({
          orgtype:orgtype
        })
      }
    })
    wx.request({
      url:app.globalData.url+ 'Organize/index',
      success:function(res){
       var  org = res.data.data
       that.setData({
         org:org
       })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})